<?php
// +----------------------------------------------------------------------
// | Fanwe 方维订餐小秘书商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------


class ctorderModule extends BizBaseModule
{
	public function __construct()
	{
		parent::__construct();
		$this->check_auth();
	}
	public function index()
	{				
		require_once APP_ROOT_PATH."app/Lib/page.php";
		$s_account_info = es_session::get("account_info");
		$account_id = intval($s_account_info['id']);
			
		$order_sn = htmlspecialchars(addslashes(trim($_REQUEST['order_sn'])));
		$timetype = intval($_REQUEST['timetype']);
		if(!empty($_REQUEST['begindate'])||!empty($_REQUEST['bd'])){
			if(!empty($_REQUEST['bd'])){
				$begindate = to_date(trim($_REQUEST['bd']),"Y-m-d");
			} 
			else{
				$begindate = htmlspecialchars(addslashes(trim($_REQUEST['begindate'])));
			}
			$GLOBALS['tmpl']->assign("begindate",$begindate);
		}
		
		if(!empty($_REQUEST['enddate'])||!empty($_REQUEST['ed'])){
			if(!empty($_REQUEST['ed'])){
				$enddate = to_date(trim($_REQUEST['ed']),"Y-m-d");
			} 
			else{
				$enddate = htmlspecialchars(addslashes(trim($_REQUEST['enddate'])));
			}
			$GLOBALS['tmpl']->assign("enddate",$enddate);
		}
		$status = intval($_REQUEST['status']);
		$deal_type = intval($_REQUEST['deal_type']);
		$order_tel = htmlspecialchars(addslashes(trim($_REQUEST['order_tel'])));
		
		if($_REQUEST['is_redirect']==1)
		{
			$url_param=array(
				"order_sn"=>$order_sn,
				"timetype"=>$timetype,
				"bd"=>to_timespan($begindate,"Y-m-d"),
				"ed"=>to_timespan($enddate,"Y-m-d"),
				//"status"=>$status,
				"order_tel"=>$order_tel,
				"deal_type"=>$deal_type,
				);
			app_redirect(url("biz","ctorder",$url_param));
		}
		
		$GLOBALS['tmpl']->assign("page_title","用户预订");
		
		$supplier_id = intval($GLOBALS['db']->getOne("select supplier_id from ".DB_PREFIX."supplier_account where id = ".intval($s_account_info['id'])));
		$GLOBALS['tmpl']->assign("supplier_id",$supplier_id);
		
		/*if(check_ipop_limit(get_client_ip(),"store_ct_order",10)){
			//将过期的订单设置为过期失败
			$GLOBALS['db']->autoExecute(DB_PREFIX."supplier_location_order",array("status"=>4,"order_tip"=>"失约"),"UPDATE","(status=0 OR status=1) AND order_time < ".get_gmtime()." AND sid=".$supplier_id);
		}*/
		
		$pageArgs = array();
		$ext_Where = " slo.sid in (".implode(",",$s_account_info['location_ids']).")";
		$tfield = " slo.order_time";
		if(!empty($_REQUEST['order_sn'])){
			$pageArgs['order_sn'] = $_REQUEST['order_sn'];
			$ext_Where .= " AND slo.order_sn = '".$_REQUEST['order_sn']."'";
			$GLOBALS['tmpl']->assign("order_sn",$_REQUEST['order_sn']);
		}
		if(intval($_REQUEST['timetype'])>0){
			$pageArgs['timetype'] = $_REQUEST['timetype'];
			if(intval($_REQUEST['timetype'])==2){
				$tfield = " slo.create_time";
			}
			if(trim($begindate)!="" && trim($enddate)!=""){
				$pageArgs['bd'] = $begindate;
				$pageArgs['ed'] = $enddate;
				$start_time = to_timespan($begindate);
				$end_time = to_timespan($enddate);
				$ext_Where .= " AND $tfield BETWEEN $start_time AND $end_time";
			}
			if(trim($begindate)!="" && trim($enddate)==""){
				$pageArgs['bd'] = $begindate;
				$start_time = to_timespan($begindate);
				$ext_Where .= " AND $tfield >= $start_time";
			}
			if(trim($begindate)=="" && trim($enddate)!=""){
				$pageArgs['ed'] = $enddate;
				$end_time = to_timespan($enddate);
				$ext_Where .= " AND $tfield <= $end_time";
			}
		}
	
		/*if(isset($_REQUEST['status'])&&$_REQUEST['status']!="-1"){
			$pageArgs['status'] = $_REQUEST['status'];
			$ext_Where .= " AND slo.status=".intval($_REQUEST['status']);
		}*/
		
		if(!empty($_REQUEST['order_tel'])){
			$ext_Where .=" AND slo.order_tel = '".$_REQUEST['order_tel']."' ";
			$GLOBALS['tmpl']->assign("order_tel",$_REQUEST['order_tel']);	
	   }
	   
	   if($deal_type > 0){
			$pageArgs['deal_type'] = $deal_type;
			$ext_Where .= " AND slo.deal_type=".($deal_type-1);
		}
		$GLOBALS['tmpl']->assign("deal_type",$deal_type);	
		
		if(!empty($_REQUEST['ct_name'])){
	   		$GLOBALS['tmpl']->assign("keyword",$_REQUEST['ct_name']);
	   		$kws_div = div_str($_REQUEST['ct_name']);
			foreach($kws_div as $k=>$item)
			{
				$kw[$k] = str_to_unicode_string($item);
			}
			$kw_unicode = implode(" ",$kw);
			//有筛选
			$ext_Where .=" and ((match(sl.name_match) against('".$kw_unicode."' IN BOOLEAN MODE)) or name like '%".$_REQUEST['ct_name']."%') ";	
	   }
	   
		$page = intval($_REQUEST['p']);
		if($page==0)
		$page = 1;
		$limit = (($page-1)*app_conf("PAGE_SIZE")).",".app_conf("PAGE_SIZE");
		
		$list = $GLOBALS['db']->getAll("SELECT slo.*,sl.name as location_name FROM ".DB_PREFIX."supplier_location_order slo LEFT JOIN ".DB_PREFIX."supplier_location sl ON sl.id =slo.sid WHERE ".$ext_Where." ORDER BY slo.create_time DESC,slo.id desc LIMIT ".$limit);
		$GLOBALS['tmpl']->assign("order_list",$list);
		$count = $GLOBALS['db']->getOne("SELECT count(DISTINCT slo.id) FROM ".DB_PREFIX."supplier_location_order slo LEFT JOIN ".DB_PREFIX."supplier_location sl ON sl.id =slo.sid WHERE ".$ext_Where);
		
		$url_parameter = http_build_query($pageArgs);
		$page = new Page($count,app_conf("PAGE_SIZE"),"&".$url_parameter);   //初始化分页对象 		
		$p  =  $page->show();
		$GLOBALS['tmpl']->assign('pages',$p);
		
		$GLOBALS['tmpl']->display("biz/biz_ct_order.html");
	}
	
	public function view()
	{
		$s_account_info = es_session::get("account_info");
		$account_id = intval($s_account_info['id']);
			
		$GLOBALS['tmpl']->assign("page_title",$GLOBALS['lang']['SUPPLIER_ORDER_VIEW']);
		
		$supplier_id = intval($GLOBALS['db']->getOne("select supplier_id from ".DB_PREFIX."supplier_account where id = ".intval($s_account_info['id'])));
		$GLOBALS['tmpl']->assign("supplier_id",$supplier_id);
		
		$order_id = intval($_REQUEST['id']);
		$order_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier_location_order where id = ".$order_id." AND sid in  (".implode(",",$s_account_info['location_ids']).") ");
	
		if($order_info)
		{
			$GLOBALS['tmpl']->assign("order_info",$order_info);
			$GLOBALS['tmpl']->display("biz/biz_ctorder_view.html");
		}
		else
		{
			showErr($GLOBALS['lang']['SUPPLIER_NOT_MATCH'],0);
		}
	}
	
	public function step1(){
		$s_account_info = es_session::get("account_info");
		$account_id = intval($s_account_info['id']);
		
		$return = array("status"=>0,"message"=>"");
		$order_id = intval($_REQUEST['id']);
		$order_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier_location_order where id = ".$order_id." AND status=0 AND sid in  (".implode(",",$s_account_info['location_ids']).") ");
	
		if($order_info)
		{
			$order_tip = htmlspecialchars(addslashes(trim($_REQUEST['order_tip'])));
			if($GLOBALS['db']->autoExecute(DB_PREFIX."supplier_location_order",array("status"=>1),"UPDATE"," id = ".$order_id)){
				//发送短信
				if($order_info['deal_type'] ==1)
					$tmpl = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."msg_template where name = 'TPL_SMS_STORE_KTV_ORDER'");
				else
					$tmpl = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."msg_template where name = 'TPL_SMS_STORE_ORDER'");
				$tmpl_content = $tmpl['content'];
				$location = $GLOBALS['db']->getRow("SELECT `name`,`address`,`tel`,`is_bank` FROM ".DB_PREFIX."supplier_location WHERE id=".$order_info['sid']);
				
				$sms_order['user_name'] = $order_info['order_name'];
				$sms_order['user_name'] .= $order_info['order_sex'] ==1 ? "先生" : "女士";
				$sms_order['location_name'] = $location['name'];
				$sms_order['order_time'] = to_date($order_info['order_time'],"m月d日H点i分");
				$sms_order['info'] = $order_info['seat_status'] == 0 ? "大厅" : "包厢" . $order_info['order_nums'] ."位";
				if($order_info['extra_serve']==1)
					$sms_order['info'] .= "，"."吸烟区";
				elseif($order_info['extra_serve']==2)
					$sms_order['info'] .= "，"."无烟区";
				
				if($location['is_bank']==1)
					$sms_order['info'] .= "，"."支持银行卡支付";
				
					
				$sms_order['address'] = $location['address'];
				$sms_order['tel'] = $location['tel'];
				
				$GLOBALS['tmpl']->assign("order",$sms_order);
				$msg = $GLOBALS['tmpl']->fetch("str:".$tmpl_content);
				
				$msg_data['send_type'] = 0;
				$msg_data['content'] = addslashes($msg);
				$msg_data['send_time'] = 0;
				$msg_data['is_send'] = 0;
				$msg_data['create_time'] = get_gmtime();
				$msg_data['user_id'] = intval($GLOBALS['user_info']['id']);
				$msg_data['is_html'] = 0;
				$msg_data['is_youhui'] = 3;
				$msg_data['dest'] = $order_info['order_tel'];
				$GLOBALS['db']->autoExecute(DB_PREFIX."deal_msg_list",$msg_data,"INSERT"); //插入 
				
				//用户自定义发送
				//短信
				$tel_msg_data['send_type'] = 0;
				$tel_msg_data['content'] = addslashes($order_info['order_sms_message']);
				$tel_msg_data['send_time'] = 0;
				$tel_msg_data['is_send'] = 0;
				$tel_msg_data['create_time'] = get_gmtime();
				$tel_msg_data['user_id'] = intval($GLOBALS['user_info']['id']);
				$tel_msg_data['is_html'] = 0;
				$tel_msg_data['is_youhui'] = 4;
				
				$tel_list = explode(";",$order_info['order_sms_tel']);
				foreach($tel_list as $k=>$v){
					if(!empty($v)){
						$tel_msg_data['dest'] = $v;
						$GLOBALS['db']->autoExecute(DB_PREFIX."deal_msg_list",$tel_msg_data,"INSERT"); //插入 
					}
				}
				//邮件
				$email_msg_data['send_type'] = 1;
				$email_msg_data['content'] = addslashes($order_info['order_sms_message']);
				$email_msg_data['send_time'] = 0;
				$email_msg_data['is_send'] = 0;
				$email_msg_data['create_time'] = get_gmtime();
				$email_msg_data['user_id'] = intval($GLOBALS['user_info']['id']);
				$email_msg_data['is_html'] = 0;
				$email_msg_data['is_youhui'] = 4;
				
				$email_list = explode(";",$order_info['order_email_address']);
				foreach($email_list as $k=>$v){
					if(!empty($v)){
						$email_msg_data['dest'] = $v;
						$GLOBALS['db']->autoExecute(DB_PREFIX."deal_msg_list",$email_msg_data,"INSERT"); //插入 
					}
				}
								
				app_redirect(url("biz","ctorder#view",array("id"=>$order_id)));
			}
			else{
				showErr($GLOBALS['lang']['CANCEL_ORDER_FAILED'],0);
			}
		}
		else
		{
			showErr($GLOBALS['lang']['SUPPLIER_NOT_MATCH'],0);
		}
	}
	
	public function daodabox(){
		$id = intval($_REQUEST['id']);
		$GLOBALS['tmpl']->assign("order_id",$id);
		$GLOBALS['tmpl']->display("biz/biz_ctorder_daoda_box.html");
	}
	
	public function do_daoda(){
		$s_account_info = es_session::get("account_info");
		$account_id = intval($s_account_info['id']);
		
		$return = array("status"=>0,"message"=>"");
		$order_id = intval($_REQUEST['order_id']);
		$order_info = $GLOBALS['db']->getRow("select id,user_id from ".DB_PREFIX."supplier_location_order where id = ".$order_id."  AND status=1 AND sid in  (".implode(",",$s_account_info['location_ids']).") ");
	
		if($order_info['id'])
		{
			$order_money = htmlspecialchars(addslashes(trim($_REQUEST['order_money'])));
			if($GLOBALS['db']->autoExecute(DB_PREFIX."supplier_location_order",array("status"=>2,"order_money"=>$order_money),"UPDATE"," id = ".$order_id)){
				$location_name = $GLOBALS['db']->getOne("SELECT sl.name FROM ".DB_PREFIX."supplier_location sl LEFT JOIN ".DB_PREFIX."supplier_location_order slo ON slo.sid=sl.id WHERE slo.id=".$order_id);
				//奖励
				$point = intval(app_conf("USER_CT_ORDER_POINT"));
				$score = intval(app_conf("USER_CT_ORDER_SCORE"));
				$money = doubleval(app_conf("USER_CT_ORDER_MONEY"));
				
				if($point>0||$score>0||$money>0)
				{
					require_once APP_ROOT_PATH."system/libs/user.php";
					$score=$score*$order_money;
					$data = array("money"=>$money,"score"=>$score,"point"=>$point);
					modify_account($data,$order_info['user_id'],"在".$location_name."就餐成功");
										
				}
				$return['score'] = intval($score);
				$return['status'] = 1;
				ajax_return($return);
			}
			else{
				$return['status'] = 0;
				$return['message'] = $GLOBALS['lang']['CANCEL_ORDER_FAILED'];
				ajax_return($return);
			}
		}
		else
		{
			$return['status'] = 0;
			$return['message'] = $GLOBALS['lang']['SUPPLIER_NOT_MATCH'];
			ajax_return($return);
		}
	}
	
	
	public function cancelbox()
	{
		$id = intval($_REQUEST['id']);
		$GLOBALS['tmpl']->assign("order_id",$id);
		$GLOBALS['tmpl']->display("biz/biz_ctorder_cancel_box.html");
	}
	
	public function do_cancel(){
		$s_account_info = es_session::get("account_info");
		$account_id = intval($s_account_info['id']);
		
		$return = array("status"=>0,"message"=>"");
		$order_id = intval($_REQUEST['order_id']);
		$order_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier_location_order where id = ".$order_id." AND sid in  (".implode(",",$s_account_info['location_ids']).") ");
	
		if($order_info)
		{
			$order_tip = htmlspecialchars(addslashes(trim($_REQUEST['order_tip'])));
			if($GLOBALS['db']->autoExecute(DB_PREFIX."supplier_location_order",array("status"=>4,"order_tip"=>$order_tip),"UPDATE"," id = ".$order_id)){
				$return['status'] = 1;
				ajax_return($return);
			}
			else{
				$return['status'] = 0;
				$return['message'] = $GLOBALS['lang']['CANCEL_ORDER_FAILED'];
				ajax_return($return);
			}
		}
		else
		{
			$return['status'] = 0;
			$return['message'] = $GLOBALS['lang']['SUPPLIER_NOT_MATCH'];
			ajax_return($return);
		}
	}
	
	
	
}

?>